package com.doraro.coal.moudle.purch.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doraro.coal.exception.R;
import com.doraro.coal.moudle.purch.entity.PurchApply;
import com.doraro.coal.moudle.purch.entity.vo.PurchApplyList;
import com.doraro.coal.moudle.purch.enums.PermitStatus;
import com.doraro.coal.moudle.purch.service.IPurchApplyService;
import com.doraro.coal.utils.ShiroUtils;
import com.doraro.coal.vo.PageParam;
import com.doraro.coal.vo.PageResult;
import com.doraro.coal.vo.UserDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 燃料采购计划申请单 前端控制器
 * </p>
 *
 * @author doraro
 * @since 2019-10-08
 */
@RestController
public class PurchApplyRestController {
    @Autowired
    private IPurchApplyService purchApplyService;

    @PostMapping("/createPR.do")
    public R doCreatePr(@RequestBody PurchApply purchApply){
        purchApplyService.saveOrUpdate(purchApply);
        return R.success();
    }
    /**
     * 只能查看自己的采购申请查询
     * @return
     */
    @PostMapping("/PRList")
    public R<PageResult<PurchApplyList>> prList(@RequestBody PageParam<PurchApply> searchMap ){

        final Page<PurchApply> pageLimit = searchMap.toPage();
        String billNumber = (String) searchMap.get("billNumber");
        Integer permitStatus = (Integer) searchMap.get("permitStatus");
        final Long userId = ShiroUtils.getUserId();
        final IPage<PurchApply> res = purchApplyService.page(pageLimit, Wrappers.<PurchApply>lambdaQuery()
                .like(StringUtils.isNotEmpty(billNumber),PurchApply::getBillNumber,billNumber)
                .eq(permitStatus != null, PurchApply::getPermitStatus,permitStatus)
                .eq(PurchApply::getCreateUserId,userId)
        );

        return R.success(getPageResult(res));
    }

    @PostMapping("/removePR.do")
    public R removePR(@RequestParam String id) {
        final PurchApply purchApply = purchApplyService.getById(id);
        if (purchApply == null) {
            return R.fail("该单据不存在");
        }
        if (!(purchApply.getPermitStatus().equals(PermitStatus.DART)
                || purchApply.getPermitStatus().equals(PermitStatus.REJECT))
        ){
            return R.fail("不能删除" + purchApply.getPermitStatus().getDesc() + "的单据");
        }
        purchApplyService.removeById(id);
        return R.success();
    }


    @PostMapping("/auditPRList")
    public R<PageResult<PurchApplyList>> auditPRList(@RequestBody PageParam<PurchApply> searchMap){
        final Page<PurchApply> pageLimit = searchMap.toPage();
        final UserDto userEntity = ShiroUtils.getUserEntity();
        final IPage<PurchApply> res = purchApplyService.page(pageLimit, Wrappers.<PurchApply>lambdaQuery()
                .eq(PurchApply::getPermitStatus, PermitStatus.ADUITING)
                .eq(PurchApply::getCreateUserOrg, userEntity.getOrganize().getName())
        );
        return R.success( getPageResult(res));
    }
    @PostMapping("pr/reject")
    public R rejectgetAudits(@RequestParam String purchId,@RequestParam String remark) {
        final UserDto userEntity = ShiroUtils.getUserEntity();
        purchApplyService.changeStatus(purchId,remark,userEntity,PermitStatus.REJECT);

        return R.success();
    }

    @PostMapping("pr/ok")
    public R ok(@RequestParam String purchId,@RequestParam String remark) {
        final UserDto userEntity = ShiroUtils.getUserEntity();
        purchApplyService.changeStatus(purchId, remark, userEntity,PermitStatus.PUBLISHED);
        return R.success();
    }



    private PageResult<PurchApplyList> getPageResult(IPage<PurchApply> res) {
        final IPage<PurchApplyList> convert = res.convert(purchApply -> {
            final PurchApplyList purchApplyList = new PurchApplyList();
            BeanUtils.copyProperties(purchApply, purchApplyList);
            purchApplyList.setStatus(purchApply.getPermitStatus().getDesc());
            return purchApplyList;
        });
        return new PageResult<>(convert.getTotal(), convert.getRecords());
    }
}

